



#!/usr/bin/env python
#coding=utf-8

import random
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest


class SendSms:

    def __init__(self, phone):

        self.phone = phone

        self.code = random.randint(100000, 999999)

        self.send_info = self.send_sms()

    def send_sms(self):
        '''发送短信'''

        client = AcsClient('LTAI4GEcU39ycMq4AC4i3oYt', 'Pu011Rk1GC6L0hFk24Gk8BYOtM2Ppv', 'cn-hangzhou')
        
        request = CommonRequest()
        request.set_accept_format('json')
        request.set_domain('dysmsapi.aliyuncs.com')
        request.set_method('POST')
        request.set_protocol_type('https') # https | http
        request.set_version('2017-05-25')
        request.set_action_name('SendSms')

        request.add_query_param('RegionId', "cn-hangzhou")
        request.add_query_param('PhoneNumbers', f"{self.phone}")
        request.add_query_param('SignName', "美美未来")
        request.add_query_param('TemplateCode', "SMS_188555689")
        request.add_query_param('TemplateParam', json.dumps({"code": f"{self.code}"}))

        response = client.do_action(request)

        return json.loads(response.decode())



if __name__ == '__main__':
    
    x = SendSms('18931604682')

    print(x.send_info)
    